package org.advanced.io;

import java.io.*;

/**
 * @author John.zhang
 * @version Id: Files, v 1.0 2020/12/14 11:24 ZhiYong Exp $
 * File整合，目录遍历
 */
public class Files {

    public static void main(String[] args) throws IOException {
        InputStreamReader inputStreamReader = new InputStreamReader(System.in);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        //用户输入的路径
        String path = null;
        try {
            path = bufferedReader.readLine();
            File file = new File(path);
            //将文件转化为文件输入流
            FileInputStream fileInputStream = new FileInputStream(file);
            if (file.isDirectory() && file.exists()) {
                //遍历子目录
                fineList(file);
            } else {
                System.out.println("文件路径输入错误");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            bufferedReader.close();
            inputStreamReader.close();
        }
    }

    private static void fineList(File filePath) {
        File[] files = filePath.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isFile()) {
                    System.out.println(file.getName());
                } else {
                    //如果是一个目录继续遍历
                    fineList(file);
                }
            }
        }
    }
}
